﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Util.Helpers
{
    /// <summary>
    /// Utility methods
    /// </summary>
    public static class Utilities
    {
        #region properties


        #endregion

        #region methods

        /// <summary>
        /// Validate provided measurements
        /// </summary>
        public static Measurement ValidateAndConvertMeasurements(string XString, string YString, string WidthString, string HeightString)
        {
            //declare
            Measurement msrmnt=null; //measurement that will be returned (converted)
            string helpString;
            int index = -1;

            try
            {
                //new measurement
                msrmnt = new Measurement();

                //X:
                if (XString != null && XString != string.Empty)
                {

                    if (XString.ToLower() == "max")
                    {
                        //get max X of the screen
                        msrmnt.X = Cache.GraphicsDevice.Viewport.Width;
                    }
                    else if (XString.Contains("max-"))
                    {
                        //get the number substructed from max, and subtract
                        index = XString.IndexOf('-');

                        helpString = XString.Substring(index, (XString.Length-1) - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('-'))
                        {
                            helpString=helpString.Replace("-","");
                        }

                        //now we have number that we want to substract from the max
                        msrmnt.X = Cache.GraphicsDevice.Viewport.Width - Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else if (XString.Contains("max/"))
                    {
                        //get max size size dividided by given number
                        index = XString.IndexOf('/');
                        helpString = XString.Substring(index, (XString.Length - 1) - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('/'))
                        {
                            helpString=helpString.Replace("/","");
                        }

                        //now we have number that we want to divide max by
                        msrmnt.X = Cache.GraphicsDevice.Viewport.Width / Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;
                    }
                    else
                    {
                        //just convert
                        msrmnt.X = Convert.ToInt32(XString);
                    }
                }
                //Y:
                if (YString != null && YString != string.Empty)
                {
                    if (YString.ToLower() == "max")
                    {
                        //get max Y of the screen
                        msrmnt.Y = Cache.GraphicsDevice.Viewport.Height;
                    }
                    else if (YString.Contains("max-"))
                    {
                        //get the number substructed from max, and subtract
                        index = YString.IndexOf('-');

                        helpString = YString.Substring(index, YString.Length - index);

                        //remove minus sign, if exist
                        if(helpString.Contains('-'))
                        {
                            helpString=helpString.Replace("-","");
                        }

                        //now we have number that we want to substract from the max
                        msrmnt.Y= Cache.GraphicsDevice.Viewport.Height - Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else if (XString.Contains("max/"))
                    {
                        //get the number substructed from max, and divide
                        index = YString.IndexOf('/');

                        helpString = YString.Substring(index, YString.Length - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('-'))
                        {
                            helpString=helpString.Replace("/","");
                        }

                        //now we have number that we want to devide max by
                        msrmnt.Y = Cache.GraphicsDevice.Viewport.Height / Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;
                    }
                    else
                    {
                        //just convert
                        msrmnt.Y = Convert.ToInt32(YString);
                    }
                }
                //Width:
                if (WidthString != null && WidthString != string.Empty)
                {
                    if (WidthString.ToLower() == "max")
                    {
                        //get width of the screen
                        msrmnt.Width = Cache.GraphicsDevice.Viewport.Width;
                    }
                    else if (WidthString.Contains("max-"))
                    {
                        //get the number substructed from max, and subtract
                        index = WidthString.IndexOf('-');

                        helpString = WidthString.Substring(index, WidthString.Length - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('-'))
                        {
                            helpString=helpString.Replace("-","");
                        }

                        //now we have number that we want to substract from the max
                        msrmnt.Width = Cache.GraphicsDevice.Viewport.Width - Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else if (XString.Contains("max/"))
                    {
                        //get the number substructed from max, and divide
                        index = WidthString.IndexOf('/');

                        helpString = WidthString.Substring(index, WidthString.Length - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('/'))
                        {
                            helpString=helpString.Replace("/","");
                        }

                        //now we have number that we want to divide max by
                        msrmnt.Width = Cache.GraphicsDevice.Viewport.Width / Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else
                    {
                        //just convert
                        msrmnt.Width = Convert.ToInt32(WidthString);
                    }
                }
                //Height:
                if (HeightString != null && HeightString != string.Empty)
                {
                    if (HeightString.ToLower() == "max")
                    {
                        //get height of the screen
                        msrmnt.Height = Cache.GraphicsDevice.Viewport.Height;
                    }
                    else if (HeightString.Contains("max-"))
                    {
                        //get the number substructed from max, and subtract
                        index = HeightString.IndexOf('-');

                        helpString = HeightString.Substring(index, HeightString.Length - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('-'))
                        {
                            helpString=helpString.Replace("-","");
                        }

                        //now we have number that we want to substract from the max
                        msrmnt.Height = Cache.GraphicsDevice.Viewport.Height - Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else if (XString.Contains("max/"))
                    {
                        //get the number substructed from max, and divide
                        index = HeightString.IndexOf('/');

                        helpString = HeightString.Substring(index, HeightString.Length - index);

                          //remove minus sign, if exist
                        if(helpString.Contains('/'))
                        {
                            helpString=helpString.Replace("/","");
                        }

                        //now we have number that we want to divide max by
                        msrmnt.Height = Cache.GraphicsDevice.Viewport.Height / Convert.ToInt32(helpString.Trim());

                        //reset help vars
                        index = -1;
                        helpString = null;

                    }
                    else
                    {
                        //just convert
                        msrmnt.Height = Convert.ToInt32(HeightString);
                    }
                }




                //return
                return msrmnt;
            }
            catch (Exception ex)
            {
                ErrorLogging.LogError(ex);
                return msrmnt;
            }
        }


        /// <summary>
        /// Check if given map cell ID
        /// is in the list of map cell infos
        /// </summary>
        /// <param name="mapCellID"></param>
        /// <returns></returns>
        public static bool ListContainsMapCell(string mapCellID, List<Levels.MapCellInfo> mapCellInfos)
        {
            try
            {
                for (int i = 0; i < mapCellInfos.Count; i++)
                {
                    if (mapCellInfos[i].MapCellID == mapCellID)
                    {
                        return true;
                    }
                }

                return false;

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return false;
            }
        }








        #region BOMB RELATED

        /// <summary>
        /// Get bomb X
        /// </summary>
        /// <returns></returns>
        public static float GetBombX()
        {
            try
            {

                for (int i = 0; i < Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs.Count; i++)
                {
                    if (Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs[i].BombID.ToLower() == World.WorldManager.BombBeingDefused.ToLower())
                    {
                        return Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs[i].Position.X;
                    }
                }


                return -1;

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return -1;
            }
        }


        /// <summary>
        /// Get bomb Y
        /// </summary>
        /// <returns></returns>
        public static float GetBombY()
        {
            try
            {

                for (int i = 0; i < Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs.Count; i++)
                {
                    if (Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs[i].BombID.ToLower() == World.WorldManager.BombBeingDefused.ToLower())
                    {
                        return Levels.LevelManager.CurrentMap.CurrentMapCell.Bombs[i].Position.Y;
                    }
                }


                return -1;

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return -1;
            }
        }

        #endregion



        #endregion


    }
}
